/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.hibernate.service.impl;

import com.wisnu.hibernate.entity.Ptik;
import com.wisnu.hibernate.error.PtikException;
import com.wisnu.hibernate.service.PtikDao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PtikDaoImpl implements PtikDao {

    private final SessionFactory sessionFactory;

    public PtikDaoImpl(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void insertPtik(Ptik ptik) throws PtikException {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.save(ptik);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new PtikException(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }

    }

    @Override
    public void updatePtik(Ptik ptik) throws PtikException {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.update(ptik);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new PtikException(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public void deletePtik(Ptik ptik) throws PtikException {
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            session.delete(ptik);
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new PtikException(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public Ptik getByNim(String nim) throws PtikException {
        Ptik ptik = null;
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            ptik = (Ptik) session.createQuery("from Ptik where nim = :nim ")
                    .setString("nim", nim)
                    .uniqueResult();
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new PtikException(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return ptik;
    }

    @Override
    public Ptik getByEmail(String email) throws PtikException {
        Ptik ptik = null;
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            ptik = (Ptik) session.createQuery("from Ptik where email = :email ")
                    .setString("email", email)
                    .uniqueResult();
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new PtikException(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return ptik;
    }

    @Override
    public List<Ptik> getAllPtik() throws PtikException {
        List<Ptik> list = new ArrayList<>();
        Transaction transaction = null;
        Session session = sessionFactory.openSession();
        try {
            transaction = session.beginTransaction();
            list = session.createQuery("from Ptik").list();
            transaction.commit();
        } catch (HibernateException exception) {
            if (transaction != null) {
                transaction.rollback();
                throw new PtikException(exception.getMessage());
            }
        } finally {
            session.flush();
            session.close();
        }
        return list;
    }

}
